//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Pattern;

//import android.util.Log;

// Class for reading CPU information
public class ReadCPU {
	
	//private static final String TAG = "Tokaplot";
	
	boolean fileExists = false;
	
	// Last values
	private boolean cp_Init = true;
	private int cp_last_Proc = 0;
	private int cp_last_Total = 0;
	private int cp_last_Work = 0;	
	
	// Pattern matchers
	private static Pattern pSpaceSplit = Pattern.compile("\\s+");
	
	public ReadCPU() {
		
		// Try and open file
		File fCPU = new File("//proc//stat");
    	if (fCPU.exists() == false) {
    		fCPU = null;
    		fileExists = false;
    	} else {
    		fileExists = true;
    	}
	}
	
	public boolean canRead() {
		return fileExists;
	}
	
	public boolean read(float [] cpu_val) {
		
		// CPU usage
		File fCPU = new File("//proc//stat");
    	if (fCPU != null && fCPU.exists()) {
			try {				
				int cp_Total = 0;
				int cp_Work = 0;
				int cp_Proc = 0;
			    String line;
			    String [] lineElements;
			    
			    // Read file
			    BufferedReader brCPU = new BufferedReader(new FileReader(fCPU));
			    while((line = brCPU.readLine()) != null) {
			    	lineElements = pSpaceSplit.split(line, 2);
					    
					// Check line head
					if (lineElements[0].matches("cpu") == true) {
						
						// Get CPU usage
						lineElements = pSpaceSplit.split(lineElements[1], 11);
				    	for (int i = 0; i < 7; i++) {
			    			cp_Total += Integer.parseInt(lineElements[i]);
			    		}
			    		cp_Work = cp_Total - Integer.parseInt(lineElements[3]); // Work = total - idle
			    		
			    	} else if (lineElements[0].matches("processes") == true) {
			    		
			    		// Get number of processes
			    		cp_Proc = Integer.parseInt(lineElements[1]);
			    	}
			    }
			    brCPU.close();
			    
			    // Calculate differences
			    if (cp_Init == false) {
			    	cpu_val[0] = (float)100.0f*(cp_Work - cp_last_Work)/(float)(cp_Total - cp_last_Total);	// Convert to %
				    cpu_val[1] = (float)(cp_Proc - cp_last_Proc);
			    }
			    
			    // Save last values
			    cp_last_Proc = cp_Proc;
			    cp_last_Total = cp_Total;
			    cp_last_Work = cp_Work;
			    cp_Init = false;
			    
			    return true;
			    
			} catch (Exception e) {
				
				// Can't read file
				//Log.e(TAG,"ReadNetwork: Exception reading CPU data file: " + e.getMessage());
			}
    	}
    	
    	// Can't find or open file
    	cp_Init = true;
    	return false;    	
	}
	
}
